package com.buaa.aimall.goodsModule.Controller;

import com.buaa.aimall.goodsModule.Entity.GoodsOrderEntity;
import com.buaa.aimall.goodsModule.DTO.FilterOrderDTO;
import com.buaa.aimall.userModule.DTO.IdDTO;
import com.buaa.aimall.userModule.Service.UserService;
import com.buaa.aimall.Utils.Enum.StatusCode;
import com.buaa.aimall.Utils.Exception.BusinessException;
import com.buaa.aimall.goodsModule.Service.GoodsOrderService;
import com.buaa.aimall.Utils.BaseResponse;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.goodsModule.DTO.UserOrderStatsDTO;
import com.buaa.aimall.goodsModule.DTO.UserOrderStatsRequestDTO;
import com.buaa.aimall.goodsModule.DTO.PayTransactionDTO;
import com.buaa.aimall.goodsModule.DTO.FilterTransactionOrderDTO;
import com.buaa.aimall.goodsModule.Entity.TransactionOrderEntity;
import com.buaa.aimall.goodsModule.Service.TransactionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.buaa.aimall.goodsModule.DTO.CreateOrdersRequestDTO;
import com.buaa.aimall.goodsModule.DTO.CreateOrdersFromCartRequestDTO;
import com.buaa.aimall.goodsModule.DTO.CalculateOrdersAmountRequestDTO;

import jakarta.validation.Valid;

import java.util.List;

@RestController
@RequestMapping("/goods/goods-order")
@CrossOrigin
public class GoodsOrderController {
    @Autowired
    GoodsOrderService goodsOrderService;
    @Autowired
    private UserService userService;
    @Autowired
    private TransactionOrderService transactionOrderService;

    @PostMapping("/create-orders")
    public BaseResponse<TransactionOrderEntity> createOrders(@Valid @RequestBody CreateOrdersRequestDTO request) {
        try {
            TransactionOrderEntity transactionOrder = goodsOrderService.createOrders(request.getOrders(), request.getModelCouponId(), request.getComputeCouponId());
            return new BaseResponse<>(StatusCode.SUCCESS, "创建订单成功", transactionOrder);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/create-orders-from-cart")
    public BaseResponse<TransactionOrderEntity> createOrdersFromCart(@Valid @RequestBody CreateOrdersFromCartRequestDTO request) {
        try {
            if (request.getCartItems() == null || request.getCartItems().isEmpty()) {
                return new BaseResponse<>(StatusCode.PARAM_ERROR, "购物车商品ID列表不能为空", null);
            }

            TransactionOrderEntity result = goodsOrderService.createOrdersFromCart(
                    request.getCartItems(),
                    request.getModelCouponId(),
                    request.getComputeCouponId()
            );

            return new BaseResponse<>(StatusCode.SUCCESS, "从购物车创建订单成功", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @GetMapping("/get-model-order-list-by-model-id")
    public BaseResponse<List<GoodsOrderEntity>> getModelOrderListByModelId(@Valid IdDTO request) {
        try {
            List<GoodsOrderEntity> result = goodsOrderService.getModelOrderListByModelId(request.getId());
            return new BaseResponse<>(StatusCode.SUCCESS, "通过模型id获取模型订单列表成功", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/filter-order")
    public BaseResponse<PageReturnResult<GoodsOrderEntity>> filterModel(
            @Valid @RequestBody FilterOrderDTO filterModelOrderDTO) {
        try {
            PageReturnResult<GoodsOrderEntity> result = goodsOrderService.filterOrder(filterModelOrderDTO);
            return new BaseResponse<>(StatusCode.SUCCESS, "查询成功", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }


    }

    @PostMapping("/get-user-order-stats")
    public BaseResponse<UserOrderStatsDTO> getUserOrderStats(@RequestBody @Valid UserOrderStatsRequestDTO request) {
        try {
            UserOrderStatsDTO result = goodsOrderService.getUserOrderStats(request.getOrderType(),
                    userService.getCurrentUser().getId());
            return new BaseResponse<>(StatusCode.SUCCESS, "获取用户订单统计信息成功", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/pay-transaction")
    public BaseResponse<Boolean> payTransaction(@Valid @RequestBody PayTransactionDTO payTransactionDTO) {
        try {
            Boolean result = goodsOrderService.payTransaction(payTransactionDTO.getTransactionId(), payTransactionDTO.getIsSuccess());
            return new BaseResponse<>(StatusCode.SUCCESS, "支付处理完成", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/filter-transaction")
    public BaseResponse<PageReturnResult<TransactionOrderEntity>> filterTransaction(
            @Valid @RequestBody FilterTransactionOrderDTO filterDTO) {
        try {
            // 获取当前用户ID并设置为查询条件
            String currentUserId = userService.getCurrentUser().getId();
            filterDTO.setBuyerID(currentUserId);

            PageReturnResult<TransactionOrderEntity> result =
                    transactionOrderService.filterTransactionOrder(filterDTO);
            return new BaseResponse<>(StatusCode.SUCCESS, "查询成功", result);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/calculate-cart-items-amount")
    public BaseResponse<Integer> calculateCartItemsAmount(@Valid @RequestBody CalculateOrdersAmountRequestDTO request) {
        try {
            Integer totalAmount = goodsOrderService.calculateCartItemsAmount(request.getOrders(), request.getModelCouponId(), request.getComputeCouponId());
            return new BaseResponse<>(StatusCode.SUCCESS, "计算购物车商品总价成功", totalAmount);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

    @PostMapping("/calculate-orders-amount")
    public BaseResponse<Integer> calculateOrdersAmount(@Valid @RequestBody CreateOrdersRequestDTO request) {
        try {
            Integer totalAmount = goodsOrderService.calculateOrdersAmount(request.getOrders(), request.getModelCouponId(), request.getComputeCouponId());
            return new BaseResponse<>(StatusCode.SUCCESS, "计算订单总价成功", totalAmount);
        } catch (BusinessException ex) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, ex.getMessage(), null);
        }
    }

}
